<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" fixed />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>

        <template #default>
          <van-image round class="avatar" :src="compilelist.photo" @click="uploadFn"/>
           <!-- file文件 -->
              <input
               type="file"
               ref="iptFile"
               v-show="false"
               accept="image/*"
               @change="onFileChange"
               />
        </template>
      </van-cell>
      <van-cell title="名称" is-link :value="compilelist.name" @click="upNamefn" />
      <!-- 名称弹出层 -->
      <van-dialog v-model="show" title="修改名称" show-cancel-button class="dahaha" :before-close="onquxiaofn" >
   <input type="text"  v-fofo v-model="username" class="haha"  placeholder="请输入名称">
</van-dialog>
      <van-cell title="性别" is-link :value="'人妖'" />
      <van-cell title="生日" is-link :value="compilelist.birthday" @click="isShowBirth=true" />
      <!-- 生日弹出层 -->

<van-popup v-model="isShowBirth" position="bottom" style="height: 50%" round> <van-datetime-picker
  v-model="currentDate"
  type="date"
  title="选择年月日"
  :min-date="minDate"
  :max-date="maxDate"
  @cancel="isShowBirth = false"
  @confirm="confirmFn"/>
</van-popup>

    </van-cell-group>
  </div>
</template>

<script>
import { userProfileAPI, updatePhotoAPI, updateProfileAPI } from '@/api/User.js'
import { Notify } from 'vant'
export default {
  name: 'UserEdit',
  data() {
    return {
      compilelist: {},
      show: false,
      username: '',
      isShowBirth: false, // 显示时间选择器
      minDate: new Date(1900, 0, 1), // 最小的可选的日期
      maxDate: new Date(), // 最大的可选日期
      currentDate: new Date() // 当前日期
    }
  },
  async created() {
    const res = await userProfileAPI()
    this.compilelist = res.data.data
  },
  methods: {
    // 上传头像触发点击事件
    uploadFn() {
      this.$refs.iptFile.click()
    },
    // 文件选择方法
    async onFileChange(el) {
      console.log(el.target.files[0])
      const fd = new FormData()
      fd.append('photo', el.target.files[0])
      const res = await updatePhotoAPI(fd)
      this.compilelist.photo = res.data.data.photo
    },
    // 点击修改名称
    upNamefn() {
      this.show = true
      this.username = this.compilelist.name
    },
    // 取消确定方法
    async  onquxiaofn(action, done) {
      if (action === 'confirm') {
        if (/^[A-Za-z0-9\u4e00-\u9fa5]{1,7}$/.test(this.username)) {
          // console.log(123)
          await updateProfileAPI({ userName: this.username })
          // console.log(res)
          this.compilelist.name = this.username
          done()
        } else {
          Notify({ type: 'warning', message: '用户名中英文和数字1-7位' })
          // 阻止弹窗关闭
          done(false)
        }
      } else {
        done()
      }
    },
    // 修改时间方法
    async confirmFn() {
      await updateProfileAPI({
        birthday: this.currentDate.toLocaleDateString()
      })
      this.compilelist.birthday = this.currentDate.toLocaleDateString()
      this.isShowBirth = false
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
.dahaha{
  text-align: center;
  .haha{
    border: none;
    text-align: center;
    font-size: 12px;
  }
}
</style>
